#!/bin/sh

ct=cleartool

#TODO:
#1. get directories to work

branch=""
path="."

if [ $# -eq 1 ] ; then
  branch="$1"
elif [ $# -eq 2 ] ; then
  branch="$1"
  path="$2"
else
  echo "usage: `basename $0` <branch> [path]"
  echo
  echo "ex.:   `basename $0` <branch> -all"
  echo "       `basename $0` <branch> /vobs/rbs/bb/dp0/rxm/swus"

  exit 1
fi

for fil in `$ct find $path -branch "brtype($branch)" -print` ; do
  if  $ct lsco $fil | grep $branch > /dev/null; then
    echo "CHECKEDOUT FILE: $fil"
    xmessage -center "CHECKEDOUT FILE: $fil"
  fi

  old_ifs=$IFS
  IFS=$'
'
  # Check if rebase has been done and compare to base on main branch instead of branch/0
  file_version_to_compare="$fil/0"
  fil_wo_atat=`echo $fil | sed 's/@@.*//g'`

  if test -d $fil_wo_atat ; then
    if ! $ct ls $fil_wo_atat | grep $branch > /dev/null ; then
      echo "It seems like there is a directory on $branch that you don't see!!"
      xmessage -center "It seems like there is a directory on $branch that you don't see!!"
    fi
  fi

  for rad in `$ct lsvtree -merge $fil_wo_atat ` ; do
    if echo $rad | grep -- "->" | grep "$branch" > /dev/null; then
      file_version_to_compare=`echo $prev_line |awk '{print $1}'`
    fi
    prev_line=$rad
  done
  unset IFS

  echo "comparing: $file_version_to_compare $fil/LATEST"
  #gvimdiff $file_version_to_compare $fil/LATEST
  cleartool diff -graphical $file_version_to_compare $fil/LATEST
  echo 
done

